package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.data.AdminStatData;
import com.cskaoyan.bean.data.RowsBean;
import com.cskaoyan.mapper.OrderGoodsMapper;
import com.cskaoyan.mapper.OrderMapper;
import com.cskaoyan.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author: yudefu
 * @date 2021年06月06日  19:45
 * @description: 统计报表模块
 */

@Service
public class AdminStatServiceImpl implements AdminStatService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderGoodsMapper orderGoodsMapper;


    @Override
    public AdminStatData getUserStat() {

        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();

        criteria.andAddTimeIsNotNull();
        criteria.andDeletedEqualTo(false);

        List<User> users = userMapper.selectByExample(userExample);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        AdminStatData adminStatData = new AdminStatData();

        List<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("users");

        adminStatData.setColumns(columns);

        List<RowsBean> rows = new ArrayList<>();

        Map<String, Integer> userStatMap = new HashMap<>();

        for (User user : users) {
            Date addTime = user.getAddTime();
            String format = dateFormat.format(addTime);

            if (!(userStatMap.containsKey(format))) {
                userStatMap.put(format, 1);
                continue;
            }
            Integer nums = userStatMap.get(format);
            nums += 1;
            userStatMap.put(format, nums);
        }

        Set<String> keySet = userStatMap.keySet();
        for (String key : keySet) {
            RowsBean rowsBean = new RowsBean();
            rowsBean.setDay(key);
            rowsBean.setUsers(userStatMap.get(key));
            rows.add(rowsBean);
        }

        adminStatData.setRows(rows);

        return adminStatData;
    }

    @Override
    public AdminStatData getOrderStat() {

        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andAddTimeIsNotNull();
        criteria.andDeletedEqualTo(false);

        List<Order> orders = orderMapper.selectByExample(orderExample);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        List<String> dates = new ArrayList<>();


        AdminStatData adminStatData = new AdminStatData();

        List<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("orders");
        columns.add("customers");
        columns.add("amount");
        columns.add("pcr");




        adminStatData.setColumns(columns);

        List<RowsBean> rows = new ArrayList<>();

        for (Order order : orders) {
            Date addTime = order.getAddTime();
            String format = dateFormat.format(addTime);
            if (dates.contains(format)) {
                continue;
            }
            dates.add(format);
        }


        for (String date : dates) {
            String start = " 00:00:00";
            String end = " 23:59:59";

            start = date + start;
            end = date + end;

            List<Order> orderList = orderMapper.selectOrdersByTimeScope(start, end);

            RowsBean rowsBean = new RowsBean();
            rowsBean.setDay(date);
            rowsBean.setOrders(orderList.size());

            Double amount = 0.0;


            HashMap<Integer, Integer> customers = new HashMap<>();
            for (Order order : orderList) {
                double actualPrice = order.getActualPrice().doubleValue();
                amount += actualPrice;
                customers.put(order.getUserId(), 1);

            }
            Set<Integer> customerNums = customers.keySet();
            rowsBean.setCustomers(customerNums.size());
            BigDecimal bg = new BigDecimal(amount);
            double formatAmount = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            rowsBean.setAmount(formatAmount);
            if (!(amount < 0.0001)) {
                double pcr = amount / customerNums.size();
                BigDecimal decimal = new BigDecimal(pcr);
                double formatPcr = decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                rowsBean.setPcr(formatPcr);
            }

            rows.add(rowsBean);
        }

        adminStatData.setRows(rows);

        return adminStatData;
    }


    @Override
    public AdminStatData getGoodsStat() {

        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
        criteria.andAddTimeIsNotNull();
        criteria.andDeletedEqualTo(false);

        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        AdminStatData adminStatData = new AdminStatData();

        List<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("orders");
        columns.add("products");
        columns.add("amount");

        adminStatData.setColumns(columns);

        List<RowsBean> rows = new ArrayList<>();
        List<String> dates = new ArrayList<>();

        for (OrderGoods orderGood : orderGoods) {
            Date addTime = orderGood.getAddTime();
            String format = dateFormat.format(addTime);
            if (dates.contains(format)) {
                continue;
            }
            dates.add(format);
        }



        for (String date : dates) {
            String start = date + " 00:00:00";
            String end = date + " 23:59:59";


            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            Date startDate = null;
            Date endDate = null;

            try {
                startDate = df.parse(start);
                endDate = df.parse(end);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            OrderGoodsExample orderGoodsExample1 = new OrderGoodsExample();
            OrderGoodsExample.Criteria criteria1 = orderGoodsExample1.createCriteria();
            criteria1.andAddTimeBetween(startDate, endDate);
            criteria1.andDeletedEqualTo(false);
            List<OrderGoods> orderGoodsList = orderGoodsMapper.selectByExample(orderGoodsExample1);

            RowsBean rowsBean = new RowsBean();
            rowsBean.setDay(date);
            rowsBean.setProducts(orderGoodsList.size());

            Double amount = 0.0;

            HashMap<Integer, Integer> orders = new HashMap<>();

            for (OrderGoods goods : orderGoodsList) {
                double goodsPrice = goods.getPrice().doubleValue();
                int goodsNums = goods.getNumber().intValue();
                Double everyAmount = goodsNums * goodsPrice;
                amount += everyAmount;

                orders.put(goods.getOrderId(), 1);
            }

            Set<Integer> orderNums = orders.keySet();
            rowsBean.setOrders(orderNums.size());
            BigDecimal bg = new BigDecimal(amount);
            double formatAmount = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            rowsBean.setAmount(formatAmount);

            rows.add(rowsBean);
        }

        adminStatData.setRows(rows);

        return adminStatData;
    }
}

